package nacos

import (
	"encoding/json"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

type AddConfig struct {
	Mysql struct {
		User     string
		Password string
		Host     string
		Port     int
		Date     string
	}
	Redis struct {
		Addr     string
		Password string
		DB       int
	}
}

var Add AddConfig

func Nacos() {
	//create clientConfig
	clientConfig := constant.ClientConfig{
		NamespaceId:         "d60d48af-4d72-47a9-827f-378703629814", //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "debug",
	}
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "console1.nacos.io",
			ContextPath: "/nacos",
			Port:        8848,
			Scheme:      "http",
		},
	}
	client, err := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})
	if err != nil {
		return
	}
	content, err := client.GetConfig(vo.ConfigParam{
		DataId: "shopconfig",
		Group:  "shop_nacos"})
	json.Unmarshal([]byte(content), &Add)
}
